/*
 * 文件名：UpLoadCarOutInfoRunble.java	 
 * 时     间：下午7:46:05
 * 作     者：lyf      
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.cloud.threads;

import java.io.IOException;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

import com.cloud.entity.ResultVo;
import com.cz073586.tools.http.HttpManagerUtil;
import com.cz073586.tools.util.JsonUtil;
import com.cz073586.tools.util.Log4jUtil;
import com.cz073586.tools.util.Tools;
import com.local.entity.vo.ParkwellPayBillVo;
import com.local.entity.vo.PayCarVo;
import com.local.manager.cfgManager.CloudConfigManager;
import com.local.util.HttpClientUtil;

/**
 * 类描述:车辆出场时把出场状态更改
 * 
 * @ClassName: UpLoadCarOutInfoRunble
 * @author Administrator
 * @date 2015年10月10日 下午7:46:05
 */
public class UploadCarPayInfoRunble implements Runnable
{
	private Log4jUtil log = Log4jUtil.getLog4j(UploadCarPayInfoRunble.class);
	private ParkwellPayBillVo parkwellPayBillVo;
	
	public UploadCarPayInfoRunble(ParkwellPayBillVo parkwellPayBillVo)
	{
		super();
		this.parkwellPayBillVo = parkwellPayBillVo;
	}
	
	@Override
	public void run()
	{
		// 发送请求
		CloseableHttpClient httpClient = HttpManagerUtil.getInstance()
				.getHttpClient(null);
		try
		{
			
			if (Tools.isEmpty(this.parkwellPayBillVo))
			{
				return;
			}
			// 获取请求路径
			String uri = CloudConfigManager.instance().getHost()
					+ CloudConfigManager.instance().getCfgValueByKey(
							"sync_pay_car");
			
			HttpPost post = new HttpPost(uri);
			post.setHeader("Content-Type", "application/json;charset=UTF-8");
			post.setConfig(HttpClientUtil.getTimeOutCfg(0));
			PayCarVo payCarVo = new PayCarVo();
			
			
			if (Tools.isEmpty(parkwellPayBillVo.getExchangeUuid()))
			{
				parkwellPayBillVo.setExchangeUuid(CloudConfigManager.instance()
						.getCfgValueByKey("exchange_id"));
			}
			
			payCarVo.setParkId(parkwellPayBillVo.getExchangeUuid());
			payCarVo.setCarNum(parkwellPayBillVo.getCarNo());
			payCarVo.setCarParkInId(parkwellPayBillVo.getCarInUuid());
			payCarVo.setCoupon(parkwellPayBillVo.getCoupon());
			payCarVo.setInTime(parkwellPayBillVo.getCarInTime().getTime());
			//payCarVo.setMoney(parkwellPayBillVo.getTheoryPrice());
			//通知自动扣费金额
			double mony =!Tools.isEmpty(parkwellPayBillVo.getPriceDifference())?parkwellPayBillVo.getPriceDifference()*10:0d;
			payCarVo.setMoney((int)mony);
			
			String sendData = JsonUtil.getJsonString(payCarVo);
			StringEntity s = new StringEntity(sendData, "UTF-8");
			post.setEntity(s);
			
			CloseableHttpResponse resp = httpClient.execute(post);
			String result = EntityUtils.toString(resp.getEntity(), "UTF-8");
			
			ResultVo resultVo = (ResultVo) JsonUtil.getObjectByJson(result,
					ResultVo.class);
			
			if (resultVo.getResult().equals("success"))
			{
				System.out.println("百事通通自动扣费成功.");
			} else
			{
				System.out.println("百事通通自动扣费失败.");
			}
			
		} catch (Throwable t)
		{
			log.error("", t);
			try
			{
				if (httpClient != null)
				{
					httpClient.close();
				}
			} catch (IOException localIOException1)
			{
			}
		} finally
		{
			try
			{
				if(null!=httpClient)
				{
				httpClient.close();
				}
			} catch (IOException e)
			{
				
			}
		}
		
	}
	
}
